package com.gdk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.BannerService;
import com.gdk.service.GoodsService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.service.TreeService;
import com.gdk.utils.XVDateUtils;


/**
 * 树controller
 * */

@Controller
public class TreeController {
	
	@Autowired
    private TreeService treeService;
	@Autowired
    private GoodsService goodsService;
	
	/**
	 * 新增树前查询
	 * */
	@RequestMapping("/preAddTree.do")
	public String preAddTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		List<Map<String, Object>> allGoods = goodsService.selectAllGoods(map);
		request.setAttribute("allGoods", allGoods);
		return "jsp/tree/tree-add";
	}
	/**
	 * 新增树
	 * */
	@ResponseBody
	@RequestMapping("/addTree.do")
	public int addTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("ft_status", 1);
		map.put("ft_createtime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("树插入  :  "+map);
		
		//map.put("ft_endtime", XVDateUtils.dateCutMillis(map.get("ft_endtime").toString()));
		
		
		int re_status = treeService.addTree(map);
		System.out.println("结果 : "+re_status);
		return re_status;
	}
	
	/**
	 * 查询所有树
	 * */
	@RequestMapping("/selectAllTree.do")
	public String selectAllTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		List<Map<String, Object>> allTree = null;
		
		allTree = treeService.selectAllTree();

		int itemCount = allTree.size();
		
		for (Map<String, Object> map2 : allTree) {
			if(map2.get("ft_createtime") != null) {
				int p = Integer.valueOf(map2.get("ft_createtime").toString());
				if(p != 0) {
					map2.put("ft_createtime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("ft_endtime") != null) {
				int p = Integer.valueOf(map2.get("ft_endtime").toString());
				if(p != 0) {
					map2.put("ft_endtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allTree",allTree);
		return "jsp/tree/tree-list";
	}
	
	/**
	 *修改前查询树详情
	 * */
	@RequestMapping("/preSelectTreeDetails.do")
	public String preSelectTreeDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> treeDetails = treeService.selectTreeDetails(map);
		
		
			/*if(treeDetails.get("ft_endtime") != null) {
				int p = Integer.valueOf(treeDetails.get("ft_endtime").toString());
				if(p != 0) {
					treeDetails.put("ft_endtime", XVDateUtils.MillisFormat(p));	
				}
			}
			*/
		List<Map<String, Object>> allGoods = goodsService.selectAllGoods(map);
		request.setAttribute("allGoods", allGoods);
		request.setAttribute("treeDetails",treeDetails);
		return "jsp/tree/tree-edit";
	}
	
	/**
	 * 修改树
	 * */
	@ResponseBody
	@RequestMapping("/updateTree.do")
	public int updateTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("树 : "+map);
		if(map.get("ft_time") !=null && !"".equals(map.get("ft_time").toString())) {
			map.put("ft_time", XVDateUtils.stringDateToMillis(map.get("ft_time").toString()));
		}
		int shop = treeService.updateTreeDetails(map);
		return shop;
	}


}